package com.efast.dmom.mes.datahandlers.entity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.Mbom;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.MbomDao;
import com.efast.dmom.mes.datahandlers.base.PartRelateDataHandler;
import com.efast.dmom.mes.entity.MbomEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;

@Component
public class MbomDataHandler extends PartRelateDataHandler<Mbom>{
	@Autowired
	private MbomDao mbomDao;
	
	@Override
	public ResultBean save(Mbom obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.setBaseEntityData(entity);
		int save = mbomDao.save(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean update(Mbom obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.updateBaseEntityData(entity);
		int save = mbomDao.update(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean delete(Mbom obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.updateBaseEntityData(entity);
		int save = mbomDao.delete(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean saveBatch(List<Mbom> obj) {
		
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Mbom> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Mbom> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Mbom getById(long id) {
		MbomEntity mbomEntity = mbomDao.getById(id);
		if(mbomEntity!=null) {
			Mbom mbom = new Mbom();
			mbom.setInnerData(mbomEntity);
			return mbom;
		}
		return null;
	}

}
